package com.excesys.exsecs.eco.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.excesys.exsecs.eco.entity.EcoCoolHourFlow;
import com.excesys.exsecs.eco.entity.EcoPowerHourFlow;
import com.excesys.exsecs.eco.mapper.EcoCoolHourFlowMapper;
import com.excesys.exsecs.eco.mapper.EcoPowerHourFlowMapper;
import com.excesys.exsecs.eco.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author lxc
 * @title: EcoTask
 * @projectName pig
 * @description: 能耗定时任务
 * @date 2023/5/1711:19
 */
@Slf4j
@Component
@AllArgsConstructor
public class EcoTask {

    private final EcoPowerHourFlowMapper ecoPowerHourFlowMapper;
    private final EcoCoolHourFlowMapper ecoCoolHourFlowMapper;

    /**
     * 节能用电量和制冷量流水定时任务
     */
   @Scheduled(cron = "0 0 * * * ?")
    private String insertEcoPowerAndCoolFlowHandler() {
        log.info("insertEcoPowerAndCoolFlowHandler------start");
        LocalDateTime date = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);

        String[] zhuji = new String[]{"9f6cc14a086b44069ee6059b27033405","6b0addbf05d841bdb0f76a60391ab21a"};
        String[] lengquebeng = new String[]{"e6e89d1b69c74caf9ab6dd098e030faf","4e81519cbe3446bda8ec89bdb74bc853"};
        String[] lengdongbeng = new String[]{"2275100b7dbe46ffa866fc555355ec3f", "4aa648e87fe24412b8d3b2b6f636e406","b401ca52fec74177b40dc7a8f5674bac","900d760d1a7a4ab296367a362103ea56","b6b6823d4bf842eea7aa122f14f4c795"};
        String[] lengqueta = new String[]{"fce19b86fe194b0f8a77af30857dd4dd","d999f4b473d24412a5aba6cac51f2d11","0e44e2fa08bf4101877efa7d90042f32",
                "4bd40b35dd3f42ada10764926c9161ce","72b1b5bc798345b38ae1a483b75d1cc0","6e65e6313a6c4b67aaf2ad0b8d271bc9","29665768d24e4654918670ff2f18c44f","3647402ea2e34d898ee3742063f975fa"};
        String[] lengdongbeng2 = new String[]{"2275100b7dbe46ffa866fc555355ec3f","b401ca52fec74177b40dc7a8f5674bac"};

        insertFlowTable2(date, zhuji);
        insertFlowTable2(date, lengquebeng);
        insertFlowTable(date, lengdongbeng2, 35, 40, 10, 15);
        insertFlowTable2(date, lengqueta);

        //制冷量
        EcoCoolHourFlow cflow = ecoCoolHourFlowMapper.selectOne(Wrappers.<EcoCoolHourFlow>lambdaQuery().orderByDesc(EcoCoolHourFlow::getTimeValue).last("limit 1"));
        Float totalVal = cflow.getTotalValue();
        //获取该小时的用电量
        Map<String, Double> incrMap = ecoPowerHourFlowMapper.sumIncrByTimeValue(date);
        //获取cop
        Float cop = 0.0f;
        Float powerIncrVal = Float.valueOf(String.format("%.2f", incrMap.get("incrValue")));
        Float incrVal = powerIncrVal * cop;
        totalVal += incrVal;
        EcoCoolHourFlow ecoCoolHourFlow = new EcoCoolHourFlow(null, null, null,
                date, Integer.parseInt(date.format(DateTimeFormatter.ofPattern("yyyyMM"))), powerIncrVal, totalVal, incrVal, cop,
                "system", LocalDateTime.now(), null, null);
        ecoCoolHourFlowMapper.insert(ecoCoolHourFlow);
        log.info("insertEcoPowerAndCoolFlowHandler------end");
        return null;
    }

    private void insertFlowTable(LocalDateTime dateBetween, String[] arr, Integer highV1, Integer highV2, Integer lowV1, Integer lowV2) {
        for (String unitNo : arr) {
            //主机用电量
            EcoPowerHourFlow pflow = ecoPowerHourFlowMapper.selectOne(Wrappers.<EcoPowerHourFlow>lambdaQuery().eq(EcoPowerHourFlow::getUnitNo, unitNo)
                    .orderByDesc(EcoPowerHourFlow::getTimeValue).last("limit 1"));
            Float totalValue = pflow.getTotalValue();
            Float incrValue = 0f;
            //判断时间段是否在8点-21点之间
            if (dateBetween.getHour() >=8 && dateBetween.getHour() <21) {
                //耗电量高的时候
                incrValue = RandomUtil.getFloatRandom(highV1, highV2);
            }else{
                incrValue = RandomUtil.getFloatRandom(lowV1, lowV2);
            }
            totalValue += incrValue;
            EcoPowerHourFlow powerHourFlow = new EcoPowerHourFlow(null, unitNo, null,
                    dateBetween, Integer.parseInt(dateBetween.format(DateTimeFormatter.ofPattern("yyyyMM"))), totalValue, totalValue, incrValue,
                    "system", LocalDateTime.now(), null, null);
            ecoPowerHourFlowMapper.insert(powerHourFlow);
        }
    }

    private void insertFlowTable2(LocalDateTime dateBetween, String[] arr) {
        for (String unitNo : arr) {
            //主机用电量
            EcoPowerHourFlow pflow = ecoPowerHourFlowMapper.selectOne(Wrappers.<EcoPowerHourFlow>lambdaQuery().eq(EcoPowerHourFlow::getUnitNo, unitNo)
                    .orderByDesc(EcoPowerHourFlow::getTimeValue).last("limit 1"));
            Float totalValue = pflow.getTotalValue();
            Float incrValue = 0f;
            EcoPowerHourFlow powerHourFlow = new EcoPowerHourFlow(null, unitNo, null,
                    dateBetween, Integer.parseInt(dateBetween.format(DateTimeFormatter.ofPattern("yyyyMM"))), totalValue, totalValue, incrValue,
                    "system", LocalDateTime.now(), null, null);
            ecoPowerHourFlowMapper.insert(powerHourFlow);
        }
    }

}
